import 'package:flutter/material.dart';
import 'package:hxdj/common/components/my_image.dart';
import 'package:hxdj/common/components/my_cache_network_image.dart';

//圆角图片
class MyClipRect extends StatelessWidget {
  final String? imageUrl;
  final String? imageName;
  final double? width;
  final double? height;
  final double? radius;
  final BoxFit? fit;
  final Widget Function(BuildContext, String)? placeholder;
  final Widget Function(BuildContext, String, Object)? errorWidget;

  const MyClipRect(
      {super.key,
      this.imageUrl,
      this.imageName = "",
      this.width,
      this.height,
      this.radius,
      this.fit = BoxFit.cover,
      this.placeholder,
      this.errorWidget});

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
        //圆角图片
        borderRadius: BorderRadius.circular(radius ?? width! / 2.0),
        child: imageName!.isNotEmpty
            ? MyImage(imageName: imageName!, width: width, height: height, boxFit: fit)
            : MyCachedNetworkImage(
                imageUrl: imageUrl!,
                width: width,
                height: width ?? height,
                fit: BoxFit.cover,
                placeholder: placeholder ??
                    (context, url) {
                      return MyClipRect(
                        imageName: "ic_chat_header_default",
                        width: width,
                        height: width ?? height,
                        radius: radius,
                      );
                    },
                errorWidget: errorWidget ??
                    (context, url, object) {
                      return MyClipRect(
                        imageName: "ic_chat_header_default",
                        width: width,
                        height: width ?? height,
                        radius: radius,
                      );
                    }
              ));
  }
}
